package com.xxc.hs.funActivity.chart;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xxc.hs.R;

import java.util.ArrayList;
import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "ChatAdapter";
    private static final int VIEW_TYPE_USER = 0;
    private static final int VIEW_TYPE_AI = 1;

    private List<Message> messages = new ArrayList<>();

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        if (viewType == VIEW_TYPE_USER) {
            View view = inflater.inflate(R.layout.item_message_user, parent, false);
            return new UserMessageViewHolder(view);
        } else {
            View view = inflater.inflate(R.layout.item_message_ai, parent, false);
            return new AiMessageViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Message message = messages.get(position);

        if (holder instanceof UserMessageViewHolder) {
            UserMessageViewHolder userHolder = (UserMessageViewHolder) holder;

            if (message.isImage()) {
                // 显示图片消息
                userHolder.textView.setVisibility(View.GONE);
                userHolder.imageView.setVisibility(View.VISIBLE);

                // 加载图片
                loadImage(message.getImagePath(), userHolder.imageView);

                // 如果有文本说明，显示文本
                if (!TextUtils.isEmpty(message.getTextContent())) {
                    userHolder.textView.setVisibility(View.VISIBLE);
                    userHolder.textView.setText(message.getTextContent());
                }
            } else {
                // 显示普通文本消息
                userHolder.textView.setVisibility(View.VISIBLE);
                userHolder.imageView.setVisibility(View.GONE);
                userHolder.textView.setText(message.getContent());
            }
        } else if (holder instanceof AiMessageViewHolder) {
            AiMessageViewHolder aiHolder = (AiMessageViewHolder) holder;

            if (message.isLoading()) {
                // 显示加载状态
                aiHolder.textView.setVisibility(View.GONE);
                aiHolder.progressBar.setVisibility(View.VISIBLE);
            } else {
                // 显示AI回复
                aiHolder.progressBar.setVisibility(View.GONE);
                aiHolder.textView.setVisibility(View.VISIBLE);
                aiHolder.textView.setText(message.getContent());
            }
        }
    }

    private void loadImage(String imagePath, ImageView imageView) {
        try {
            // 异步加载图片
            new Thread(() -> {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4; // 降低图片质量
                Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

                if (bitmap != null) {
                    imageView.post(() -> imageView.setImageBitmap(bitmap));
                }
            }).start();
        } catch (Exception e) {
            Log.e(TAG, "加载图片失败", e);
        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    @Override
    public int getItemViewType(int position) {
        return messages.get(position).getType();
    }

    public void addMessage(Message message) {
        messages.add(message);
        notifyItemInserted(messages.size() - 1);
    }

    public Message getItem(int position) {
        return messages.get(position);
    }

    public static class UserMessageViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;

        public UserMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.messageTextView);
            imageView = itemView.findViewById(R.id.messageImageView);
        }
    }

    public static class AiMessageViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ProgressBar progressBar;

        public AiMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.messageTextView);
            progressBar = itemView.findViewById(R.id.loadingProgressBar);
        }
    }
}